$(document).ready(function () {
    queryMark();
    selectFailGrade();
    //selectMark();
    courseType();
    //chart();
    $("#courseType").change(function () {
        var opt = $("#courseType").val();
        couresName(opt)
    });
});

//条件查询成绩
function selectMark() {
    $(document).on('click', '#selectMark', function () {
        //var $stuInfoId=5;
        var stuInfoId = $("#StuInfoId").val();
        var termYear = $("#termYear option:selected").val();
        var termNum = $("#termNum option:selected").val();
        var courseType = $("#courseType option:selected").val();
        var courseId = $("#courseId").val();
        var courseName = $("#courseName").val();
        var url = "http://localhost:8080/mark/selectMark?stuInfoId=" + stuInfoId + "&termYear=" + termYear + "&termNum=" + termNum + "&courseType=" + courseType + "&courseId=" + courseId + "&courseName=" + courseName;
        var table = $('#dataTable').dataTable()
        if (table) {
            table.fnDestroy();
        }
        $('#dataTable').dataTable({
            "processing": true,
            "serverSide": true,
            "bInfo": false,//是否显示是否启用底边信息栏
            "sAjaxDataProp": "data.markList",
            "sAjaxSource": url,
            "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
            "fnPreDrawCallback": function (oSettings) {
                $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                $('.dataTables_length select').addClass('form-control input-small');
            },
            "oLanguage": {
                "sSearch": "",
                "sLengthMenu": "<span>_MENU_ entries</span>",
                "sZeroRecords": "暂无相关信息",
            },
            "bJQueryUI": false,
            'bAutoWidth': false,
            "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
            "aoColumnDefs": [
                {
                    "mDataProp": null,
                    "sDefaultContent": "",
                    "aTargets": [-1]
                }
            ],
            "aoColumns": [
                {"mDataProp": "stuNum"},
                {"mDataProp": "stuName"},
                {"mDataProp": "termYear"},
                {"mDataProp": "termNum"},
                {"mDataProp": "courseId"},
                {"mDataProp": "courseName"},
                {"mDataProp": "courseType"},
                {"mDataProp": "courseCredithour"},
                {"mDataProp": "mark"},
            ],
        })

    });
}

//条件查询不及格成绩
function selectFailGrade() {
    $(document).on('click', '#selectFailGrade', function () {
        //var $stuInfoId=5;
        var stuInfoId = $("#StuInfoId").val();
       // alert(stuInfoId)
        var termYear = $("#termYear option:selected").val();
        var termNum = $("#termNum option:selected").val();
        var courseType = $("#courseType option:selected").val();
        var courseId = $("#courseId").val();
        var courseName = $("#courseName").val();
        var url = "http://localhost:8080/mark/selectFailGrade?stuInfoId=" + stuInfoId + "&termYear=" + termYear + "&termNum=" + termNum + "&courseType=" + courseType + "&courseId=" + courseId + "&courseName=" + courseName;
        var table = $('#dataTable').dataTable()
        if (table) {
            table.fnDestroy();
        }
        $('#dataTable').dataTable({
            "processing": true,
            "serverSide": true,
            "bInfo": false,//是否显示是否启用底边信息栏
            "sAjaxDataProp": "data.markList",
            "sAjaxSource": url,
            "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
            "fnPreDrawCallback": function (oSettings) {
                $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                $('.dataTables_length select').addClass('form-control input-small');
            },
            "oLanguage": {
                "sSearch": "",
                "sLengthMenu": "<span>_MENU_ entries</span>",
                "sZeroRecords": "暂无相关信息",
            },
            "bJQueryUI": false,
            'bAutoWidth': false,
            "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
            "aoColumnDefs": [
                {
                    "mDataProp": null,
                    "sDefaultContent": "",
                    "aTargets": [-1]
                }
            ],
            "aoColumns": [
                {"mDataProp": "stuNum"},
                {"mDataProp": "stuName"},
                {"mDataProp": "termYear"},
                {"mDataProp": "termNum"},
                {"mDataProp": "courseId"},
                {"mDataProp": "courseName"},
                {"mDataProp": "courseType"},
                {"mDataProp": "courseCredithour"},
                {"mDataProp": "mark"},
            ],
        })

    });
};


//成绩查询页面--所有成绩
var queryMark = function () {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.markList",
        "sAjaxSource": "http://localhost:8080/mark/selectMark",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": "termYear"},
            {"mDataProp": "termNum"},
            {"mDataProp": "courseId"},
            {"mDataProp": "courseName"},
            {"mDataProp": "courseType"},
            {"mDataProp": "courseCredithour"},
            {"mDataProp": "mark"},
        ],
    })

};

//下拉列表获取课程类别
function courseType() {

    var cType = $("#courseType");
  //  if(cType)
    $.ajax({
        url: "http://localhost:8080/mark/selectCourseType",
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            var str = "";
            d = data.data;
            xdh = d[1]
            for (x in xdh) {
                str += "<option value='" + xdh[x].courseType + "'>" + xdh[x].courseType + "</option>"
            }
            cType.append(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
}

//通过课程类型查看课程名称
function couresName(courseType) {
    var courseType = $("#courseType option:selected").val();
    if (courseType==null||courseType=="") {
        reloadPage()
    }
    var courseName = $("#courseName")
    $.ajax({
        url: "http://localhost:8080/mark/selectCourseNameByCoursetype?courseType=" + courseType,
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            // alert("aa")
            var str = "";
            d = data.data.courseList;
            for (x in d) {
                str += "<option value='" + d[x].courseName + "'>" + d[x].courseName + "</option>"
            }
            courseName.html(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
};

//折线图
function chart() {

    var termYear = $("#termYear option:selected").val();
    var termNum = $("#termNum option:selected").val();
    var courseId = $("#courseId").val();
    var courseName = $("#courseName").val();

    var myChart = echarts.init(document.getElementById('chartmain'));
    //使用制定的配置项和数据显示图表
    myChart.setOption({
        title: {
            text: courseName
        },
        tooltip: {},
        legend: {
            data: ['成绩']
        },
        xAxis: {
            data: ["100-95", "94-90", "89-85", "84-80","79-75", "74-70", "69-65", "64-60", "60以下"]  //x轴
        },
        yAxis: {},
        series: [{
            name: '访问量',
            type: 'line',
            data: []    //y轴
        }]
    });
    myChart.showLoading();
    var names = [];//x轴
    var nums = [];//y轴
    $.ajax({
        url: "http://localhost:8080/mark/selectCount?courseId=" + courseId + "&courseName=" + courseName + "&termYear=" + termYear + "&termNum=" + termNum,
        type: "GET",
        async: true,
        data: {},
        dataType: "json",
        success: function (data) {
            var ninety = data.data.markCount.ninety;
            var eighty = data.data.markCount.eighty;
            var seventy = data.data.markCount.seventy;
            var sixty = data.data.markCount.sixty;
            var ninety5 = data.data.markCount.ninety5;
            var eighty5 = data.data.markCount.eighty5;
            var seventy5 = data.data.markCount.seventy5;
            var sixty5 = data.data.markCount.sixty5;
            var fifty = data.data.markCount.fifty;
            nums.push(ninety5,ninety,eighty5, eighty,seventy5, seventy,sixty5, sixty, fifty)
            myChart.hideLoading();    //隐藏加载动画
            myChart.setOption({        //加载数据图表
                /* xAxis: {
                     data: names
                 },*/
                series: [{
                    // 根据名字对应到相应的系列
                    name: '成绩',
                    data: nums
                }]
            });
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
}
function wxy(){
    var ninety = data.data.markCount.ninety;
    var eighty = data.data.markCount.eighty;
    var seventy = data.data.markCount.seventy;
    var sixty = data.data.markCount.sixty;
    var ninety5 = data.data.markCount.ninety5;
    var eighty5 = data.data.markCount.eighty5;
    var seventy5 = data.data.markCount.seventy5;
    var sixty5 = data.data.markCount.sixty5;
    var fifty = data.data.markCount.fifty;
    var alls = ninety+eighty+seventy+sixty+ninety5+eighty5+eighty5+seventy5+seventy5+sixty5+fifty


    var ninety1 = ((data.data.markCount.ninety)/alls)*100;
    var eighty1 = ((data.data.markCount.eighty)/alls)*100;
    var seventy1= ((data.data.markCount.seventy)/alls)*100;
    var sixty1 = ((data.data.markCount.sixty)/alls)*100;
    var ninety51 = ((data.data.markCount.ninety5)/alls)*100;
    var eighty51 = ((data.data.markCount.eighty5)/alls)*100;
    var seventy51 = ((data.data.markCount.seventy5)/alls)*100;
    var sixty51 = ((data.data.markCount.sixty5)/alls)*100;
    var fifty1 = ((data.data.markCount.fifty)/alls)*100;

}
function chart2() {
    var termYear = $("#termYear option:selected").val();
    var termNum = $("#termNum option:selected").val();
    var courseId = $("#courseId").val();
    var courseName = $("#courseName").val();
    $.ajax({
        url: "http://localhost:8080/mark/selectCount?courseId=" + courseId + "&courseName=" + courseName + "&termYear=" + termYear + "&termNum=" + termNum,
        type: "GET",
        async: true,
        data: {},
        dataType: "json",
        success: function (data) {
            var ninety = data.data.markCount.ninety;
            var eighty = data.data.markCount.eighty;
            var seventy = data.data.markCount.seventy;
            var sixty = data.data.markCount.sixty;
            var ninety5 = data.data.markCount.ninety5;
            var eighty5 = data.data.markCount.eighty5;
            var seventy5 = data.data.markCount.seventy5;
            var sixty5 = data.data.markCount.sixty5;
            var fifty = data.data.markCount.fifty;
            var alls = ninety+eighty+seventy+sixty+ninety5+eighty5+eighty5+seventy5+seventy5+sixty5+fifty
            var ninety1 = ((data.data.markCount.ninety)/alls)*100;
            var eighty1 = ((data.data.markCount.eighty)/alls)*100;
            var seventy1= ((data.data.markCount.seventy)/alls)*100;
            var sixty1 = ((data.data.markCount.sixty)/alls)*100;
            var ninety51 = ((data.data.markCount.ninety5)/alls)*100;
            var eighty51 = ((data.data.markCount.eighty5)/alls)*100;
            var seventy51 = ((data.data.markCount.seventy5)/alls)*100;
            var sixty51 = ((data.data.markCount.sixty5)/alls)*100;
            var fifty1 = ((data.data.markCount.fifty)/alls)*100;


            var options = {
                title: {
                    text: courseName
                },
                subtitles: [{
                    text: ""
                }],
                animationEnabled: true,
                data: [{
                    type: "pie",
                    startAngle: 40,
                    toolTipContent: "<b>{label}</b>: {y}%",
                    showInLegend: "true",
                    legendText: "{label}",
                    indexLabelFontSize: 16,
                    indexLabel: "{label} - {y}%",
                    dataPoints: [
                        { y: fifty1.toFixed(2), label: "不及格" },
                        { y: sixty1.toFixed(2), label: "60-64" },
                        { y: sixty51.toFixed(2), label: "65-69" },
                        { y: seventy1.toFixed(2), label: "70-74" },
                        { y: seventy51.toFixed(2), label: "75-79" },
                        { y: eighty1.toFixed(2), label: "80-84" },
                        { y: eighty51.toFixed(2), label: "85-89" },
                        { y: ninety1.toFixed(2), label: "90-94" },
                        { y: ninety51.toFixed(2), label: "95-100" },

                    ]
                }]
            };
            $("#chartContainer").CanvasJSChart(options);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
}

//刷新页面
function reloadPage() {
    window.location.reload();
}